package com.kobeliu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kobeliu.exceptions.GraceException;
import com.kobeliu.mapper.IndustryMapper;
import com.kobeliu.pojo.Industry;
import com.kobeliu.result.ResponseStatusEnum;
import com.kobeliu.service.IndustryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class IndustryServiceImpl implements IndustryService {


    @Autowired
    private IndustryMapper industryMapper;

    @Override
    public boolean getIndustryIsExistByName(String nodeName) {
        Industry name = industryMapper.selectOne(new QueryWrapper<Industry>().eq("name", nodeName));

        return name!=null;
    }

    @Transactional
    @Override
    public void createIndustry(Industry industry) {
        industryMapper.insert(industry);
    }

    @Override
    public List<Industry> getTopIndustryList() {
        return industryMapper.selectList(new QueryWrapper<Industry>()
                                        .eq("father_id",0)
                                        .orderByAsc("sort"));
    }

    @Override
    public List<Industry> getChildIndustryList(String fatherId) {
        return industryMapper.selectList(new QueryWrapper<Industry>()
                .eq("father_id",fatherId)
                .orderByAsc("sort"));
    }

    @Transactional
    @Override
    public void updateNode(Industry industry) {
        industryMapper.updateById(industry);
    }

    @Transactional
    @Override
    public void deleteNode(String nodeName) {

        Industry name = industryMapper.selectOne(new QueryWrapper<Industry>().eq("name", nodeName));

        //分类不存在
        if(name == null) GraceException.display(ResponseStatusEnum.SYSTEM_INDEX_OUT_OF_BOUNDS);

        industryMapper.delete(new QueryWrapper<Industry>().eq("father_id",name.getId()));
        industryMapper.deleteById(name);
    }
}
